package com.bytedance.article.common.a;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.os.SystemClock;
import android.view.Choreographer;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import inline.ScreenshotListener;

public final class MonitorFPS {
    private static final Long a;
    private static final Long b;
    private static final Long c;

    static {
        Long v0 = Long.valueOf(200);
        a = v0;
        b = Long.valueOf(v0.longValue() * 1000 * 1000);
        c = Long.valueOf(1000);
    }

    private a$a d;
    private WindowManager e;
    private volatile boolean f;
    private String g;
    private Choreographer.FrameCallback h;

    public MonitorFPS(Context arg3, String arg4) {
        this.d = null;
        this.e = null;
        this.f = false;
        this.g = arg4;
        if (Build.VERSION.SDK_INT < 16) {
            this.e = (WindowManager) arg3.getSystemService(Context.WINDOW_SERVICE);
            this.d = new a$a(this, arg3);
        }
    }

    static boolean a(MonitorFPS arg1) {
        return arg1.f;
    }

    static a$a b(MonitorFPS arg1) {
        return arg1.d;
    }

    static Long c() {
        return MonitorFPS.b;
    }

    static String c(MonitorFPS arg1) {
        return arg1.g;
    }

    static Long d() {
        return MonitorFPS.c;
    }

    static boolean d(MonitorFPS arg1) {
        arg1.f = false;
        return false;
    }

    static ScreenshotListener e() {
        return null;
    }

    static Long f() {
        return MonitorFPS.a;
    }

    public synchronized final void a() {
        if (!this.f) {
            return;
        }

        this.f = false;
        if (Build.VERSION.SDK_INT >= 16) {
            if (this.h == null) {
                return;
            }
            Choreographer.getInstance().removeFrameCallback(this.h);
            return;
        }

        try {
            this.e.removeView(this.d);
            a$a.a(this.d);
            a$a.b(this.d);
        } catch (Exception v0_1) {
        }
        return;
    }

    public final void b() {
        int v1 = -2;
        if (!this.f) {
            this.f = true;
            if (Build.VERSION.SDK_INT < 16) {
                a$a.a(this.d);
                WindowManager.LayoutParams v0 = new WindowManager.LayoutParams(v1, v1, 2005, 0, -3);
                v0.gravity = 51;
                v0.flags = 24;
                v0.height = 1;
                v0.width = 1;
                try {
                    this.e.removeView(this.d);
                } catch (Exception v1_1) {
                }

                this.e.addView(this.d, ((ViewGroup.LayoutParams) v0));
                this.d.postDelayed(new cc(this), 10);
            } else {
                this.h = new dd(this);
                Choreographer.getInstance().postFrameCallback(this.h);
            }
        }
    }

    public final static class a$a extends View {
        private final MonitorFPS c;
        private long a;
        private int b;

        public a$a(MonitorFPS arg3, Context arg4) {
            super(arg4);
            this.c = arg3;
            this.a = -1;
            this.b = 0;
        }

        static long a(a$a arg2) {
            arg2.a = -1;
            return -1;
        }

        static int b(a$a arg1) {
            arg1.b = 0;
            return 0;
        }

        protected final void onDraw(Canvas arg5) {
            if (this.a == -1) {
                this.a = SystemClock.elapsedRealtime();
                this.b = 0;
            }

            long v0 = SystemClock.elapsedRealtime() - this.a;
            if (v0 > MonitorFPS.f().longValue()) {
                double v0_1 = (((double) this.b)) / (((double) v0)) * (((double) MonitorFPS.d().longValue()));
                MonitorFPS.e();
                android.support.design.a.c(MonitorFPS.c(this.c), "fps", ((float) v0_1));
                this.c.a();
            }

            ++this.b;
        }
    }
}

